package scut.kent.shopping.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import scut.kent.shopping.dao.DaoFactory;
import scut.kent.shopping.idao.ICategoryDao;
import scut.kent.shopping.model.Category;
import scut.kent.shopping.util.RequestUtil;

public class CategoryServlet extends BaseServlet{

	ICategoryDao cateDao = DaoFactory.getCategoryDao();
	/**
	 * 
	 */
	private static final long serialVersionUID = -3610845231100199343L;
	
	public String add(HttpServletRequest req,HttpServletResponse resp){
		Category cate = (Category)RequestUtil.getObject(Category.class, req);
		cateDao.add(cate);
		return "redirect:category.do?method=list";
	}
	
	public String list(HttpServletRequest req,HttpServletResponse resp){
		List<Category> list = cateDao.list();
		req.setAttribute("categories", list);
		return "Admin/Category/index.jsp";
	}
	
	public String addInput(HttpServletRequest req,HttpServletResponse resp){
		return "Admin/Category/addinput.jsp";
	}
	
	public String updateInput(HttpServletRequest req,HttpServletResponse resp){
		int id = Integer.parseInt(req.getParameter("id"));
		Category cate=cateDao.load(id);
		
		req.setAttribute("category", cate);
		return "Admin/Category/updateInput.jsp";
	}
	
	public String update(HttpServletRequest req,HttpServletResponse resp){
		Category cate = (Category)RequestUtil.getObject(Category.class, req);
		cateDao.update(cate);
		return "redirect:category.do?method=list";
	}
	
	public String delete(HttpServletRequest req,HttpServletResponse resp){
		int id = Integer.parseInt(req.getParameter("id"));
		cateDao.delete(id);
		return "redirect:category.do?method=list";
	}
}
